
var HotlinkParser = {
    MARK_KEY_1: "#key.1#",

    MARK_TEXT: "#text#",

    PATTERN_SITE_MAP: new Array(
    new Array("([hH][Tt][Tt][Pp][Ss]?://([\\-\\w\\.]+)+(:\\d+)?(?:/[^ \\\"\\t\\n\\r\\u0100-\\uffff<]*)?)", "<a class=\"hotlink\" href=\"#key.1#\" target=\"_foreign\" title=\"#text#\"><img src=\"templates/default/images/ext_link.png\" alt=\"::URL::\" width=\"14px\" height=\"14px\"></img></a>"),
    new Array("[\(]#(\\d+)[\)]", "<a class=\"hotlink\" href=\"http://www.htkou.net/forum/index.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@htkou", "<a class=\"hotlink\" href=\"http://www.htkou.net/forum/index.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@rolia", "<a class=\"hotlink\" href=\"http://www.rolia.net/f/post.php?pno=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("[\(]#(\\d+)@0[\)]", "<a class=\"hotlink\" href=\"http://www.rolia.net/f/post.php?pno=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#(\\d+)@yantange", "<a class=\"hotlink\" href=\"http://yantange.com/forum/showpost.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("[\(]y(\\d+)[\)]", "<a class=\"hotlink\" href=\"http://yantange.com/forum/showpost.php?id=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#([\\w\\s]+)@google", "<a class=\"hotlink\" href=\"http://www.google.ca/search?q=#key.1#\" target=\"_forgein\">#text#</a>"),
    new Array("#([\\w\\s]+)@youtube", "<a class=\"hotlink\" href=\"http://www.youtube.com/watch?v=#key.1#\" target=\"_forgein\">#text#</a>")
  ), 
    run: function() {
        this.convertPosts();
    },

    convertPosts: function() {
        var contentElement = document.getElementById("content");
        var posts = contentElement.getElementsByTagName("span");
        var aposts = contentElement.getElementsByTagName("a");

        for (var i = 0; i < aposts.length; i++) {
            if (aposts[i].className.indexOf('thread_nobg') >= 0
        || aposts[i].className.indexOf('threadnew') >= 0
        || aposts[i].className.indexOf('replynew') >= 0
        || aposts[i].className.indexOf('reply visited') >= 0
        || aposts[i].className.indexOf('thread visited') >= 0
         || aposts[i].className == 'reply'
         || aposts[i].className == 'thread'
      ) {
                this.convertPost(aposts[i]);
            } else if (aposts[i].className.indexOf('reply_nobg') >= 0) {
                aposts[i].innerHTML = aposts[i].childNodes[0].innerHTML;
                this.convertPost(aposts[i]);
            }
        }

        for (var i = 0; i < posts.length; i++) {
            if (posts[i].className.indexOf('thread_no_text') >= 0
      || posts[i].className.indexOf('threadnew_no_text') >= 0
      || posts[i].className.indexOf('thread-sticky_no_text') >= 0
      || posts[i].className.indexOf('threadnew-sticky_no_text') >= 0
      || posts[i].className.indexOf('replynew_no_text') >= 0
      || posts[i].className.indexOf('actthread') >= 0
      || posts[i].className.indexOf('actreply') >= 0
      || posts[i].className.indexOf('reply_no_text') >= 0) {
                this.convertPost(posts[i]);
            }
        }
    },

    convertPost: function(postElement, debug) {
        var changed = false;
        var post = postElement.innerHTML;

        for (var i = 0; i < this.PATTERN_SITE_MAP.length; i++) {
            var pattern = this.PATTERN_SITE_MAP[i][0];
            var template = this.PATTERN_SITE_MAP[i][1];

            var reg = new RegExp(pattern, "g");
            var result = reg.exec(post);
            var results = new Array();
            while (result != null) {
                results[results.length] = result;
                result = reg.exec(post);
            }

            var index = 0;
            for (var j = 0; j < results.length; j++) {
                var link = template.replace(this.MARK_KEY_1, results[j][1])
                           .replace(this.MARK_TEXT, results[j][0]);
                index = post.indexOf(results[j][0], index);
                var before = post.substring(0, index);
                var after = post.substring(index, post.length);
                //alert("before: " + before);
                //alert("after: " + after);
                post = before + after.replace(results[j][0], link);
                index = index + link.length;
                //alert("post: " + post);
                //alert("index: " + index);
                changed = true;
            }
        }

        if (changed) {
            var postlink = postElement.href;
            var postclass = postElement.className;
            var func = postElement.onclick;
            if (postElement.tagName.toLowerCase() == 'span') {
                postElement.innerHTML = post;
            } else if (postElement.tagName.toLowerCase() == 'a') {
                var news = document.createElement('span');
                news.innerHTML = post;
                for (var k = 0; k < news.childNodes.length; k++) {
                    if (news.childNodes[k].nodeType == 3) {  //TextNode
                        var newa = document.createElement('a');
                        newa.href = postlink;

                        if (func) {
                            var f = func.toString();
                            var s = f.indexOf("{");
                            var f = f.substring(s + 1, f.length - 1);
                            var click_func = new Function(f);
                            newa.onclick = click_func;
                            newa.onfocus = function() { this.blur(); };
                        }
                        newa.target = '_blank';
                        newa.innerHTML = news.childNodes[k].data;
                        news.replaceChild(newa, news.childNodes[k]);
                    }
                }
                news.className = postclass;
                postElement.parentNode.replaceChild(news, postElement);
            }
        }

    }
}

HotlinkParser.run();
